<?php

namespace backend\modules\classify\controllers;

use app\model\Order;
use common\models\classify\Donation;
use common\models\classify\News;
use common\models\user\Member;
use Yii;
use common\models\base\SearchModel;
use common\traits\Curd;

use common\enums\StatusEnum;
use backend\controllers\BaseController;


/**
 * 公益捐赠管理
 *
 * Class RequirementController
 * @package backend\modules\member\controllers
 * @author jianyan74 <751393839@qq.com>
 */
class DonationController extends BaseController
{
    use Curd;

    /**
     * @var \yii\db\ActiveRecord
     */
    public $modelClass = Donation::class;

    /**
     *
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        //$id = Yii::$app->request->get();
        //var_dump(11);die();
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['id', 'title'], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query
            ->andWhere(['>=', 'status', StatusEnum::DISABLED]);
        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel
        ]);
    }
    public function actionEdit()
    {
        $arr = Yii::$app->request->get();
       // var_dump($arr);die();
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post())) {
            $model->or_id=$arr['id'];
            if ($model->save()){

                $data = \common\models\classify\Order::findOne($arr['id']);
                $data->type = 3;
                $arr = Member::findOne($data['order_name']);

                $b =$arr['member_name'].'用户向'.$model['donation_area'].'捐赠了一批物资';
                $a = new News();
                $a->content =$b;
                $a->save();
                if ($data->save()){

                    return $this->message("捐赠成功", $this->redirect(['order/index']));
                }
            }

        }

        return $this->render($this->action->id, [
            'model' => $model,

        ]);
    }


}
